// 随机选择背景图片
const backgroundImages = [
    'background/bg1.jpg',
    'background/bg2.jpg',
    'background/bg3.jpg'
    'background/bg4.jpg'
];

function setRandomBackground() {
    const randomImage = backgroundImages[Math.floor(Math.random() * backgroundImages.length)];
    document.getElementById('backgroundImage').src = randomImage;
}

// 更新时间
function updateTime() {
    const now = new Date();
    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    document.getElementById('currentTime').textContent = `${hours}:${minutes}`;
}

// 更新电池电量（模拟）
function updateBatteryLevel() {
    const batteryLevel = Math.floor(Math.random() * 100);
    document.getElementById('batteryLevel').textContent = `${batteryLevel}%`;
}

// 随机文本数据
const randomTexts = [
    { text: "给时光以生命，给岁月以文…", translation: "本当の声を響かせてよ" },
    { text: "给时光以色彩，给岁月以歌…", translation: "心の声を響かせてよ" },
    { text: "给时光以温度，给岁月以诗…", translation: "夢の声を響かせてよ" },
    { text: "给时光以光影，给岁月以画…", translation: "未来の声を響かせてよ" },
    { text: "给时光以旋律，给岁月以舞…", translation: "希望の声を響かせてよ" }
];

// 切换内容
function switchContent() {
    const randomIndex = Math.floor(Math.random() * randomTexts.length);
    document.getElementById('randomText').textContent = randomTexts[randomIndex].text;
    document.getElementById('randomTranslation').textContent = randomTexts[randomIndex].translation;
}

// 下翻功能
function scrollToNextSection() {
    const nextSection = document.getElementById('nextSection');
    nextSection.scrollIntoView({ behavior: 'smooth' });
}

// 初始化
setRandomBackground();
updateTime();
updateBatteryLevel();
switchContent();

// 每秒更新时间
setInterval(updateTime, 1000);

// 每分钟更新电池电量
setInterval(updateBatteryLevel, 60000);

// 获取页面元素
const visitorCountElement = document.getElementById('visitorCount');
const trafficCountElement = document.getElementById('trafficCount');
const userActivityCountElement = document.getElementById('userActivityCount');

// 从后端获取数据并更新页面
fetch('lib/php/get_visitor_count.php')
   .then(response => response.json())
   .then(data => {
        visitorCountElement.textContent = data.visitor_count;
    });

fetch('lib/php/get_traffic.php')
   .then(response => response.json())
   .then(data => {
        trafficCountElement.textContent = data.traffic_count + " KB";
    });

fetch('lib/php/get_user_activity.php')
   .then(response => response.json())
   .then(data => {
        userActivityCountElement.textContent = data.user_activity_count;
    });

// 初始化流量统计图表
const trafficChartCanvas = document.getElementById('trafficChart');
const trafficChart = new Chart(trafficChartCanvas, {
    type: 'line',
    data: {
        labels: ["1月", "2月", "3月", "4月", "5月", "6月"],
        datasets: [{
            label: '流量统计',
            data: [100, 200, 150, 300, 250, 350], // 这里假设的数据，需要从后端获取
            borderColor: 'rgba(75, 192, 192, 1)',
            backgroundColor: 'rgba(75, 192, 192, 0.2)',
            fill: true,
            lineTension: 0.1
        }]
    },
    options: {
        responsive: true,
        scales: {
            x: {
                type: 'category',
                labels: ["1月", "2月", "3月", "4月", "5月", "6月"]
            },
            y: {
                beginAtZero: true
            }
        }
    }
});

// 初始化用户活跃度图表
const userActivityChartCanvas = document.getElementById('userActivityChart');
const userActivityChart = new Chart(userActivityChartCanvas, {
    type: 'line',
    data: {
        labels: ["1月", "2月", "3月", "4月", "5月", "6月"],
        datasets: [{
            label: '用户活跃度',
            data: [50, 60, 40, 70, 65, 75], // 这里假设的数据，需要从后端获取
            borderColor: 'rgba(255, 99, 132, 1)',
            backgroundColor: 'rgba(255, 99, 132, 0.2)',
            fill: true,
            lineTension: 0.1
        }]
    },
    options: {
        responsive: true,
        scales: {
            x: {
                type: 'category',
                labels: ["1月", "2月", "3月", "4月", "5月", "6月"]
            },
            y: {
                beginAtZero: true
            }
        }
    }
});
